package test.webmagic;

import java.util.List;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class GithubRepoPageProcessor implements PageProcessor {

    // 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    // process是定制爬虫逻辑的核心接口，在这里编写抽取逻辑
    public void process(Page page) {
        System.out.println("page: " + page.getUrl());
        Selectable xpath = page.getHtml().xpath("//li[@id]/a/span/text()");
        List<String> matchesElements = xpath.all();
        System.out.println("matchesElements：" + matchesElements.size());
        System.out.println(matchesElements);
//        page.addTargetRequests(page.getHtml().links().regex("(https://www\\.primefaces\\.org/showcase/[\\w\\-]+/[\\w\\-]+)").all());
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
//        System.setProperty("javax.net.debug", "all");
//        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");

        Spider.create(new GithubRepoPageProcessor())
                //从"https://github.com/code4craft"开始抓
                .addUrl("https://www.primefaces.org/showcase/ui/csv/basic.xhtml")
                //开启5个线程抓取
                .thread(1)
                //启动爬虫
                .run();
    }
}